package Thread;

import com.sun.org.apache.bcel.internal.generic.I2F;
import sun.security.jca.GetInstance;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:34
 */
public class Single {
//    //一般的双重检查
//    private static Single instance;
//
//    //一般的会出现重排序的一个问题 不为null但还没有初始化
//    public static Single getInstance(){
//        if (instance == null){
//            synchronized (Single.class){
//                if (instance == null){
//                    instance = new Single();
//                }
//            }
//        }
//        return instance;
//    }

    //使用volatile来实现延迟初始化的方案
    private volatile static Single instance;

    //这是通过禁止volatile前后的重排序来实现的
    public static Single getInstance(){
        if (instance == null){
            synchronized (Single.class){
                if (instance == null){
                    instance = new Single();
                }
            }
        };
        return instance;
    }

    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        String s = "sss";
        String ss = new String("sss");
        String jvs = ss;
        System.out.println(jvs == ss);
    }

}
